﻿namespace ToolUtil
{
    using System;
    using System.IO;
    using System.Net;
    using System.Windows.Forms;

    public class FtpClientService
    {
        private NetworkCredential certificate;

        public FtpClientService(string UserName, string PassWord)
        {
            this.certificate = new NetworkCredential(UserName, PassWord);
        }

        private int CopyDataToDestination(Stream sourceStream, Stream destinationStream, int offSet, ProgressBar Pro)
        {
            byte[] buffer = new byte[0x400];
            long length = sourceStream.Length;
            long num2 = 0L;
            Pro.Maximum = (int)length;
            double num3 = (length / 0x400L) / 0x400L;
            int count = sourceStream.Read(buffer, 0, 0x400);
            offSet = 1;
            while (count != 0)
            {
                Application.DoEvents();
                Pro.Value = (int)num2;
                destinationStream.Write(buffer, 0, count);
                count = sourceStream.Read(buffer, 0, 0x400);
                num2 += 0x400L;
            }
            destinationStream.Close();
            sourceStream.Close();
            return offSet;
        }

        private FtpWebRequest CreateFtpWebRequest(Uri uri, string method)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
            request.Proxy = null;
            request.Credentials = this.certificate;
            request.KeepAlive = true;
            request.UseBinary = true;
            request.UsePassive = true;
            request.Method = method;
            return request;
        }

        public bool UploadFile(string sourceFile, Uri destinationPath, int offSet, string ftpMethod, ProgressBar Pro)
        {
            try
            {
                Application.DoEvents();
                FileInfo info = new FileInfo(sourceFile);
                Uri uri = new Uri(destinationPath.AbsoluteUri + "/" + info.Name);
                FtpWebRequest request = this.CreateFtpWebRequest(uri, ftpMethod);
                request.ContentOffset = offSet;
                Stream requestStream = request.GetRequestStream();
                FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
                int length = (int)sourceStream.Length;
                Application.DoEvents();
                Pro.Maximum = length;
                Pro.Value = offSet;
                offSet = this.CopyDataToDestination(sourceStream, requestStream, offSet, Pro);
                request.GetResponse().Close();
                if ((offSet != 0) && (offSet != 1))
                {
                    this.UploadFile(sourceFile, destinationPath, offSet, "APPE", Pro);
                }
            }
            catch (Exception exception)
            {
                exception.ToString();
                MessageBox.Show(exception.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            return true;
        }
    }
}

